package guoanmaker.provider.business.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.provider.business.model.ProvideArea;
import guoanmaker.provider.business.model.ProvideCity;
import guoanmaker.provider.business.model.ProviderProvince;
import guoanmaker.provider.business.model.ToperatorCity;
import guoanmaker.provider.business.repository.ProvideAreaRepository;
import guoanmaker.provider.business.repository.ProvideCityRepository;
import guoanmaker.provider.business.repository.ProvideProvinceRepository;
import guoanmaker.provider.business.repository.ToperatorCityRepository;

@Service
public class ProvideProCiAreService implements ProvideProCiAreServiceIntegerface {
	@Autowired
	private ProvideAreaRepository provideAreaRepository;
	@Autowired
	private ProvideCityRepository provideCityRepository;
	@Autowired
	private ProvideProvinceRepository provideProvinceRepository;
	@Autowired
	private ToperatorCityRepository toperatorCityRepository;
	
	public List<ProviderProvince> selectprovice(){
		return provideProvinceRepository.findAll();
	}

	@Override
	public List<ProvideCity> selectcity(String procode) {
		return provideCityRepository.findBycode(procode);
		
	}

	@Override
	public List<ProvideArea> selectarea(String citycode) {
		// TODO Auto-generated method stub
		return provideAreaRepository.findBycode(citycode);
	}

	//通过省级表主键获取省对象
	@Override
	public ProviderProvince selectByID(String id) {
		ProviderProvince province=provideProvinceRepository.findOne(id);
		return province;
	}

	//通过地级市id获取地级市对象
	@Override
	public ProvideCity selectByCityID(String id) {
		ProvideCity city=provideCityRepository.findOne(id);
		return city;
	}

	/**
	 * 查找开通服务的省份
	 */
	@Override
	public List<ProviderProvince> selectOprtorProvice() {
		//List<ToperatorCity> operatorCitys=toperatorCityRepository.findByStatus(0);
		List<ToperatorCity> operatorCitys=toperatorCityRepository.findAll();
		List<ProviderProvince> provinces=null;
		if(operatorCitys.size()>0){
			List<String>  strList=new ArrayList<String>();
			for(int i=0;i<operatorCitys.size();i++){
				strList.add(operatorCitys.get(i).getProvinceId());
			}
			provinces=provideProvinceRepository.findByCodeIn(strList);
		}
		return provinces;
	}

	/**
	 * 通过省份主键查找开通服务的地级市列表
	 * @param procode
	 * @return 城市列表
	 */
	@Override
	public List<ProvideCity> selectByProviceId(String procode) {
		List<ProvideCity> citys=null;
		if(procode!=null){
			List<ToperatorCity> operatorCitys=toperatorCityRepository.findByProviceCode(procode);
			List<String> strList=new ArrayList<>();
			if(operatorCitys.size()>0){
				for(int i=0;i<operatorCitys.size();i++){
					strList.add(operatorCitys.get(i).getCityId());
				}
			}
			citys=provideCityRepository.findByCodeIn(strList);
		}
		return citys;
	}
	

}
